package com.study.english.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.study.english.pojo.vo.SysRouteVo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.study.english.domain.SysRoute;
import com.study.english.service.ISysRouteService;
import com.study.english.mapper.SysRouteMapper;
import org.springframework.stereotype.Service;

/**
 * @author wf
 * @description 针对表【sys_route】的数据库操作Service实现
 * @createDate 2024-03-14 08:57:12
 */
@Service
public class SysRouteServiceImpl extends ServiceImpl<SysRouteMapper, SysRoute>
        implements ISysRouteService {

    @Autowired
    private ISysRouteService sysRouteService;


    @Override
    public void addSysRoute(SysRouteVo sysRouteVo) {
        SysRoute sysRoute = new SysRoute();
        BeanUtils.copyProperties(sysRouteVo, sysRoute);
        sysRoute.setIsDelete(0);
        sysRoute.setCreateTime(new Date());
        sysRouteService.save(sysRoute);
    }

    @Override
    public IPage<SysRouteVo> getSysRoutePageList(IPage<SysRouteVo> page, SysRouteVo sysRouteVo) {
        return this.baseMapper.getSysRoutePageList(page, sysRouteVo);
    }


    @Override
    public List<SysRouteVo> getSysRouteList(SysRouteVo sysRouteVo) {
        return this.baseMapper.getSysRouteList(sysRouteVo);

    }

    @Override
    public List<SysRouteVo> getSysRouteTreeList() {
        SysRouteVo sysRouteVo = new SysRouteVo();
        List<SysRouteVo> departCommonVoList = this.baseMapper.getSysRouteList(sysRouteVo);
        sysRouteVo.setParentId(0L);
        List<SysRouteVo> sysDepartParentTop = this.baseMapper.getSysRouteList(sysRouteVo);
        //递归树
        for (SysRouteVo sysDepartCommonVo : sysDepartParentTop) {
            List<SysRouteVo> resultList = buildDepartTree(departCommonVoList, sysDepartCommonVo.getId());
            if (resultList == null || resultList.size() == 0) {
                resultList = null;
            }
            sysDepartCommonVo.setChildren(resultList);
        }
        return sysDepartParentTop;
    }

    //构建树
    public List<SysRouteVo> buildDepartTree(List<SysRouteVo> sysDepartCommonVoList, Long parentId) {
        List<SysRouteVo> departCommonVoList = new ArrayList<>();
        for (SysRouteVo sysDepartCommonVo : sysDepartCommonVoList) {
            if (sysDepartCommonVo.getParentId().equals(parentId)) {
                List<SysRouteVo> departCommonVoChildren = buildDepartTree(sysDepartCommonVoList, sysDepartCommonVo.getId());
                if (departCommonVoChildren == null || departCommonVoChildren.size() == 0) {
                    departCommonVoChildren = null;
                }
                sysDepartCommonVo.setChildren(departCommonVoChildren);
                departCommonVoList.add(sysDepartCommonVo);

            }
        }
        return departCommonVoList;
    }

    @Override
    public SysRoute getSysRouteById(String id) {
        return sysRouteService.getById(id);
    }


    @Override
    public void delSysRoute(String id) {
        sysRouteService.removeById(id);
    }


    @Override
    public void updateSysRoute(SysRouteVo sysRouteVo) {
        SysRoute sysRoute = new SysRoute();
        BeanUtils.copyProperties(sysRouteVo, sysRoute);
        sysRouteService.updateById(sysRoute);
    }
}




